#!/bin/bash

tmpf="keystone.patch.0"
patchf="keystone.patch"

dir1="$1"

# rever to official version
yum remove -y keystone-hotfix

diff -upN /usr/share/openstack-dashboard/openstack_dashboard/dashboards/identity/projects/workflows.py $dir1/horizon/workflows.py > $tmpf
diff -upN /usr/lib/python2.7/site-packages/keystone/resource/controllers.py $dir1/resource/controllers.py >> $tmpf
diff -upN /usr/lib/python2.7/site-packages/keystone/identity/controllers.py $dir1/identity/controllers.py >> $tmpf
diff -upN /usr/lib/python2.7/site-packages/keystone/identity/routers.py $dir1/identity/routers.py >> $tmpf
diff -upN /usr/lib/python2.7/site-packages/keystone/v2_crud/admin_crud.py $dir1/v2_crud/admin_crud.py >> $tmpf
diff -upN /usr/lib/python2.7/site-packages/keystone/v2_crud/user_crud.py $dir1/v2_crud/user_crud.py >> $tmpf

replace=false
srcfile=
echo -n "" > $patchf

IFS=$'\n';for x in $(cat $tmpf);do 
  unset IFS
  if $replace; then
    if [ "${x:0:4}" != "+++ " ]; then
        echo "FATAL ERROR"
        exit 1
    fi
    replace=false
    toend=$(echo $x |awk '{for(i=3;i<=NF;i++) {printf $i " "}}')
    echo "+++ $srcfile $toend" >> $patchf
    continue
  fi
  echo "$x" >> $patchf
  if [ "${x:0:4}" == "--- " ]; then
    srcfile=$(echo "$x" |awk '{print $2}')
    replace=true
  fi
done < $tmpf

echo "$patchf created."

